﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using VIBlend.WPF.Controls;
using System.ComponentModel;

namespace AnonsaiWPF.CSharp.ListBoxDemos
{
    public partial class ListBoxOverview : Window, IWindow
    {
        public ListBoxOverview()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ListBoxDemo_Loaded);
            this.checkBox1.Unchecked += new RoutedEventHandler(checkBox1_Unchecked);
            this.checkBox2.Unchecked += new RoutedEventHandler(checkBox2_Unchecked);

            this.PrepareGridData();
            this.ListBox.ItemsSource = this.listSaleRecord;
        }

        public void DetachContent()
        {
            RemoveLogicalChild(Content);
        }

    
        public void SetTheme()
        {
        }

        public Grid Root
        {
            get
            {
                return this.RootVisual;
            }
        }

        void checkBox1_Unchecked(object sender, RoutedEventArgs e)
        {
            this.ListBox.SelectionMode = SelectionMode.Single;
        }

        void ListBoxDemo_Loaded(object sender, RoutedEventArgs e)
        {
            this.spinEditor2.ValueChanged += new EventHandler<SpinEditorValueChangedEventArgs>(spinEditor2_ValueChanged);
            this.spinEditor1.ValueChanged += new EventHandler<SpinEditorValueChangedEventArgs>(spinEditor1_ValueChanged);
        }

        void spinEditor1_ValueChanged(object sender, SpinEditorValueChangedEventArgs args)
        {
            if (this.ListBox == null)
                return;

            if (this.spinEditor1.Value < 1)
            {
                this.spinEditor1.Value = 1;
            }

            if (this.spinEditor1.Value > 5)
            {
                this.spinEditor1.Value = 5;
            }

            this.ListBox.ColumnsCount = (int)this.spinEditor1.Value;
        }

        void spinEditor2_ValueChanged(object sender, SpinEditorValueChangedEventArgs args)
        {
            if (this.ListBox == null)
                return;

            if (this.spinEditor2.Value < 150)
            {
                this.spinEditor2.Value = 150;
            }

            if (this.spinEditor2.Value > 300)
            {
                this.spinEditor2.Value = 300;
            }

            this.ListBox.ColumnsWidth = (double)this.spinEditor2.Value;
        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.comboBox1 == null)
                return;

            switch (comboBox1.SelectedIndex)
            {
                case 0:
                    this.ListBox.LayoutMode = LayoutModes.Vertical;
                    this.spinEditor1.IsEnabled = false;
                    this.spinEditor2.IsEnabled = false;
                    break;
                case 1:
                    this.ListBox.LayoutMode = LayoutModes.VerticalWrap;
                    this.spinEditor1.IsEnabled = false;
                    this.spinEditor2.IsEnabled = true;
                    break;
                case 2:
                    this.ListBox.LayoutMode = LayoutModes.HorizontalWrap;
                    this.spinEditor1.IsEnabled = false;
                    this.spinEditor2.IsEnabled = true;
                    break;
                case 3:
                    this.ListBox.LayoutMode = LayoutModes.Columns;
                    this.spinEditor1.IsEnabled = true;
                    this.spinEditor2.IsEnabled = true;
                    break;
            }
        }

        private void checkBox1_Checked(object sender, RoutedEventArgs e)
        {
            if (this.ListBox == null)
                return;

            this.ListBox.SelectionMode = SelectionMode.Multiple;
        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (this.ListBox == null)
                return;

            this.ListBox.SearchText = this.textBox1.Text;
        }

        private void checkBox2_Checked(object sender, RoutedEventArgs e)
        {
            if (this.ListBox == null)
                return;

            this.ListBox.ShowOnlyFilteredItems = checkBox2.IsChecked.Value;
        }

        void checkBox2_Unchecked(object sender, RoutedEventArgs e)
        {
            if (this.ListBox == null)
                return;

            this.ListBox.ShowOnlyFilteredItems = checkBox2.IsChecked.Value;
        }

        private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.ListBox == null)
                return;

            if (this.comboBox2 == null)
                return;

            switch (comboBox2.SelectedIndex)
            {
                case 0:
                    this.ListBox.FilterMode = EditFilterMode.Contains;
                    break;
                case 1:
                    this.ListBox.FilterMode = EditFilterMode.StartsWith;
                    break;
            }
        }


        #region Sample Data

        #region Data
        string[] firstNames = new string[] 
        {
            "Andrew",
            "Nancy",
            "Shelley",
            "Regina",
            "Yoshi",
            "Antoni",
            "Mayumi",
            "Ian",
            "Peter",
            "Lars",
            "Petra",
            "Martin",
            "Sven",
            "Elio",
            "Beate",
            "Cheryl",
            "Michael",
            "Guylène"
        };

        string[] lastNames = new string[] 
        {
            "Fuller",
            "Davolio",
            "Burke",
            "Murphy",
            "Nagase",
            "Saavedra",
            "Ohno",
            "Devling",
            "Wilson",
            "Peterson",
            "Winkler",
            "Bein",
            "Petersen",
            "Rossi",
            "Vileid",
            "Saylor",
            "Björn",
            "Nodier"
        };

        static string[,] productNames = new string[,] 
        {
            {"Black Tea", "1.95"},
            {"Green Tea", "1.95"},
            {"Caffè Espresso", "1.45"},
            {"Doubleshot Espresso", "1.75"},
            {"Caffè Latte", "2.25"},
            {"White Chocolate Mocha", "2.35"},
            {"Caramel Latte", "2.35"},
            {"Caffè Americano", "1.65"},
            {"Cappuccino", "2.10"},
            {"Espresso Truffle", "2.45"},
            {"Espresso con Panna", "1.81"},
            {"Peppermint Mocha Twist", "1.99"}
        };

        /// <exclude/>   
        public class EmployeeSaleRecord
        {
            /// <summary>
            /// Initializes a new instance of the <see cref="EmployeeSaleRecord"/> class.
            /// </summary>
            /// <param name="Name">The name.</param>
            /// <param name="Date">The date.</param>
            /// <param name="ProductId">The product id.</param>
            /// <param name="Quantity">The quantity.</param>
            public EmployeeSaleRecord(string Name, DateTime Date, int ProductId, int Quantity)
            {
                this._name = Name;
                this._date = Date;
                this._productid = ProductId;
                this._quantity = Quantity;
            }

            #region Private Members
            private string _name;
            private DateTime _date;
            private int _productid;
            private int _quantity;
            #endregion

            #region Properties

            /// <summary>
            /// Gets or sets the name.
            /// </summary>
            /// <value>
            /// The name.
            /// </value>
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }

            /// <summary>
            /// Gets or sets the date.
            /// </summary>
            /// <value>
            /// The date.
            /// </value>
            public DateTime Date
            {
                get { return _date; }
                set { _date = value; }
            }

            /// <summary>
            /// Gets the product.
            /// </summary>
            public string Product
            {
                get { return productNames[_productid, 0]; }
            }

            /// <summary>
            /// Gets or sets the quantity.
            /// </summary>
            /// <value>
            /// The quantity.
            /// </value>
            public int Quantity
            {
                get { return _quantity; }
                set { _quantity = value; }
            }

            /// <summary>
            /// Gets the unit price.
            /// </summary>
            public double UnitPrice
            {
                get
                {
                    double unitPrice = double.Parse(productNames[_productid, 1]);
                    return unitPrice;
                }
            }

            /// <summary>
            /// Gets the sales amount.
            /// </summary>
            public double SalesAmount
            {
                get { return (double)_quantity * UnitPrice; }
            }


            #endregion
        }


        #endregion

        /// <exclude/>
        public List<EmployeeSaleRecord> listSaleRecord = new List<EmployeeSaleRecord>();

        private void PrepareGridData()
        {
            const int rowsToLoad = 50;
            listSaleRecord.Clear();

            Random rand = new Random();

            for (int i = 0; i < rowsToLoad; i++)
            {
                int productId = rand.Next(0, productNames.Length / 2 - 1);
                int quantity = rand.Next(1, 5);
                listSaleRecord.Add(new EmployeeSaleRecord(
                    string.Format("{0} {1}",
                    firstNames[rand.Next(0, firstNames.Length - 1)],
                    lastNames[rand.Next(0, lastNames.Length - 1)]),
                    DateTime.Now.AddDays(-rand.Next(10, 100)),
                    productId,
                    quantity)
                    );
            }
        }

        #endregion

        bool isAscending;
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            isAscending = !isAscending;

            ListSortDirection sortDirection = isAscending ? ListSortDirection.Ascending : ListSortDirection.Descending;
            this.ListBox.Sort(new SortDescription("Name", sortDirection));
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            this.ListBox.RemoveSort();
            this.isAscending = false;
        }
    }
}
